package com.genghis.ptas.postreply.dao.impl;

import com.genghis.core.page.dao.PageDao;
import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.postreply.dao.PostReplyDao;
import com.genghis.ptas.postreply.entity.PostReply;
import com.genghis.ptas.scorm.entity.ScormInfoRomMapper;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *
 * @author yutong
 * @version 1.0.1
 */

@Repository("postReplyDao")
public class PostReplyDaoImpl extends PageDao implements PostReplyDao {

    @Override
    public int addPostReply(PostReply postReply) {
        String sql="INSERT INTO post_reply(reply_content,reply_person_id,reply_time,post_id) VALUES(:replyContent,:replyPersonId,:replyTime,:postId)";
        KeyHolder keyHolder=new GeneratedKeyHolder();
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(postReply), keyHolder);
        return keyHolder.getKey().intValue();
    }

    @Override
    public List<PostReply> findPostReplyPageList(PageParameter pageParameter, PostReply postReply,int postId) {
        String sql="SELECT p.`topic`,p.`post_time`,u.`login_name` showLoginName ,r.* FROM posts p,post_reply r,user_info u WHERE p.`id`= r.post_id AND p.`post_person_id`=u.`id` AND r.post_id="+postId;
        return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<PostReply>(PostReply.class));
    }

    @Override
    public int delAllReply(int postId) {
        String sql="DELETE FROM post_reply WHERE post_id=?" ;
        return getJdbcTemplate().update(sql,postId);
    }

    @Override
    public int delOneReplyById(int id) {
        String sql="DELETE FROM post_reply WHERE id=?";
        return getJdbcTemplate().update(sql,id);
    }
}